<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        div {
            position: absolute;
            left: 0;
            width: 100px;
            height: 100px;
            background-color: skyblue;
        }
    </style>
</head>

<body>

    <button id='btn3'>开启</button>
    <div>一路向北</div>

    <script>
        //   点击按钮， div 移动，到了目的地，div 颜色变化 
        let btn = document.querySelector('button')
        let div = document.querySelector('div')

        btn.addEventListener('click', function () {
           move(div,300,callback)
        });

        function move(obj, target, callback) {
            clearInterval(obj.timer)
            timer = setInterval(() => {
                let left = obj.offsetLeft;
                if (left == target) {
                    clearInterval(obj.timer)
                    if (callback) {
                        callback()
                    }
                    return
                }
                let pee = (target - left) / 10
                pee = pee > 0 ? Math.ceil(pee) : Math.floor(pee);
                obj.style.left = left + pee + 'px'
            }, 100);
        }

        function callback() {
            div.style.backgroundColor = 'red'
            console.log(123);
        }
    </script>
</body>

</html>